package com.gscitysfy.cus.drain.modules.duty.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cloudfrmwk.portal.feign.UserService;
import com.gscitysfy.cloudfrmwk.portal.vo.UserInfoVO;

import com.gscitysfy.cus.drain.annotations.DictTranslation;
import com.gscitysfy.cus.drain.annotations.FieldParam;
import com.gscitysfy.cus.drain.constants.DicConstant;
import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnPeople;
import com.gscitysfy.cus.drain.modules.duty.service.IMoperDutyMnPeopleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 葛守跃
 * @version 1.0.0
 * @ClassName MoperDutyMnPeopleController
 * @Description 值班人员相关接口
 * @creatTime 2022-03-31 16:47:17
 */
@RestController
@RequestMapping("/duty/people")
@Slf4j
@Tag(name = "值班人员管理")
public class MoperDutyMnPeopleController {

    @Resource
    private IMoperDutyMnPeopleService peopleService;

    @Resource
    private UserService userService;

    /**
     * 值班人员列表查询
     * @param page
     * @param people
     * @return
     */
    @GetMapping("/getDutyPeopleListByCondition")
    @Operation(summary = "值班人员列表查询", description = "多条件查询人员列表，支持分页")
    @DictTranslation({@FieldParam(dictType = DicConstant.DUTY_TYPE, targetField = "coamPersonType"),
            @FieldParam(dictType = DicConstant.SYSTEM_FLAG, targetField = "coamSysFlag"),
            @FieldParam(dictType = DicConstant.GENDER_TYPE, targetField = "coamPersonGender")})
    public Result<IPage<MoperDutyMnPeople>> getDutyPeopleListByCondition(Page<MoperDutyMnPeople> page, MoperDutyMnPeople people){
        IPage<MoperDutyMnPeople> peopleList =  peopleService.getDutyPeopleListByCondition(page, people);
        return Result.content(peopleList);
    }

    /**
     * 获取统一门户用户列表、分页
     * @param people
     * @return
     */
    @GetMapping("/getUnifiedPortalUserList")
    @Operation(summary = "获取统一门户用户列表、分页", description = "多条件查询人员列表，支持分页")
    @DictTranslation({@FieldParam(dictType = DicConstant.GENDER_TYPE, targetField = "gender")})
    public Result<Page<UserInfoVO>> getUnifiedPortalUserList(Page<MoperDutyMnPeople> page, MoperDutyMnPeople people){
        return this.userService.getUserPage(page, people.getCoamPersonUsername(), people.getCoamPersonLoginAccount(), people.getCoamPersonContactNumber(), null);
    }

    /**
     * 添加值班人员
     * @param ids
     * @return
     */
    @PostMapping("/addDutyPerson")
    @Operation(summary = "获取统一门户用户列表、分页", description = "多条件查询人员列表，支持分页")
    public Result<String> addDutyPerson(String ids){
        if(StringUtils.isEmpty(ids)){
           return Result.failure("用户id不可为空", StrUtil.EMPTY);
        }
        if (StringUtils.isNotEmpty(peopleService.addDutyPerson(ids))){
            return Result.content(peopleService.addDutyPerson(ids));
        }else{
            return Result.failure("保存失败");
        }

    }

    /**
     * @auther: wuxinxin
     * @date: 2022/7/29 15:33
     * @Description:  值班人员基础信息更新
     * @param userList
     * @return com.gscitysfy.cloudfrmwk.assistant.domain.Result<java.lang.String>
     */
    @PostMapping("/updateDutyPersonByUserInfoVO")
    @Operation(summary = "值班人员基础信息更新", description = "值班人员基础信息更新")
    public Result<String> updateDutyPersonByUserInfoVO(@RequestBody List<UserInfoVO> userList){
        log.info("值班人员基础信息更新数据：{}", JSONUtil.toJsonStr(userList));
        if(Objects.isNull(userList)){
            return Result.failure("用户信息不可为空", StrUtil.EMPTY);
        }
        for (UserInfoVO user : userList){
            peopleService.updateDutyPersonByUserInfoVO(user);
        }
        return Result.success();
    }

    /**
     * 值班人员设置
     * @param people
     * @return
     */
    @PostMapping("/setDutyPerson")
    @Operation(summary = "值班人员设置")
    public Result<Boolean> setDutyPerson(MoperDutyMnPeople people){
        return Result.content(this.peopleService.updateById(people));
    }

    /**
     * 导出值班人员信息
     * @param people
     */
    @GetMapping("/exportDutyPerson")
    @Operation(summary = "导出值班人员信息")
    public void exportDutyPerson(HttpServletResponse response, MoperDutyMnPeople people) throws IOException {
        if(!StringUtils.isEmpty(people.getIdStr())){
            List<String> ids = Arrays.asList(people.getIdStr().split(StrUtil.COMMA));
            people.setIds(ids);
        }
        this.peopleService.exportDutyPerson(response, people);
    }

    /**
     * 校验人员编码code是否已经存在
     * @param people
     */
    @PostMapping("/checkCodeExist")
    @Operation(summary = "校验人员编码code是否已经存在")
    public Result<MoperDutyMnPeople> checkCodeExist(@RequestBody MoperDutyMnPeople people){
        LambdaQueryWrapper<MoperDutyMnPeople> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MoperDutyMnPeople::getCoamPersonOrder, Long.parseLong(people.getCoamPersonOrder().toString()));
        if(!StringUtils.isEmpty(people.getCoamKid())){
            queryWrapper.eq(MoperDutyMnPeople::getCoamKid, people.getCoamKid());
        }
        if(!StringUtils.isEmpty(people.getCoamPersonType())){
            queryWrapper.eq(MoperDutyMnPeople::getCoamPersonType, people.getCoamPersonType());
        }
        return Result.content(this.peopleService.getBaseMapper().selectOne(queryWrapper));
    }

    /**
     * 删除值班人员
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public Result delete(String ids){
        if(peopleService.deleteByIds(ids)){
            return Result.success();
        }else {
            return Result.failure("该人员已关联使用，不可删除，可以选择停用处理");
        }

    }

    /**
     * 获取值班人员详情
     * @param id
     * @return
     */
    @GetMapping("/getDutyUserInfo")
    @Operation(summary = "获取值班人员详情")
    @DictTranslation({@FieldParam(dictType = DicConstant.DUTY_TYPE, targetField = "coamPersonType")})
    public Result<MoperDutyMnPeople> getDutyUserInfo(@RequestParam String id){
        LambdaQueryWrapper<MoperDutyMnPeople> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MoperDutyMnPeople::getCoamPersonId, id);
        return Result.content(this.peopleService.getOne(queryWrapper));
    }


}
